package org.opencv.samples.tutorial4;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener;
import org.opencv.highgui.Highgui;


import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;


public class Sample4Mixed extends Activity implements CvCameraViewListener {
    private static final String    TAG = "OCVSample::Activity";

    private static final int       VIEW_MODE_RGBA     = 0;
    private static final int       VIEW_MODE_GRAY     = 1;
    private static final int       VIEW_MODE_CANNY    = 2;
    private static final int       VIEW_MODE_FEATURES = 5;

    private int                    mViewMode;
    private int					selectfilter;
    private int					mHeight;
    private int					mWidth;

    
    
    private Mat                    mRgb;
    private Mat                    mIntermediateMat;
    private Mat                    mGrayMat;
    private MenuItem               mItemPreviewRGBA;
    private MenuItem               mItemPreviewGray;
    private MenuItem               mItemPreviewCanny;
    private MenuItem               mItemPreviewFeatures;


    private CameraBridgeViewBase   mOpenCvCameraView;

    private BaseLoaderCallback  mLoaderCallback = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
                case LoaderCallbackInterface.SUCCESS:
                {
                    Log.i(TAG, "OpenCV loaded successfully");

                    // Load native library after(!) OpenCV initialization
                    System.loadLibrary("mixed_sample");
                
                    mOpenCvCameraView.enableView();
                } break;
                default:
                {
                    super.onManagerConnected(status);
                } break;
            }
        }
    };

    public Sample4Mixed() {
        Log.i(TAG, "Instantiated new " + this.getClass());
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.i(TAG, "called onCreate");
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.tutorial4_surface_view);


        mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.tutorial4_activity_surface_view);
        mOpenCvCameraView.setCvCameraViewListener(this);

        mOpenCvCameraView.setMaxFrameSize(800, 800);
        mOpenCvCameraView.setMinimumHeight(200);
        mOpenCvCameraView.setMinimumWidth(200);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        Log.i(TAG, "called onCreateOptionsMenu");
        mItemPreviewRGBA = menu.add("Preview RGBA");
        mItemPreviewGray = menu.add("Threshold");
        mItemPreviewCanny = menu.add("Thres + Ero + Dil");
        mItemPreviewFeatures = menu.add("Find features");
        return true;
    }

    @Override
    public void onPause()
    {
        if (mOpenCvCameraView != null)
            mOpenCvCameraView.disableView();
        super.onPause();
    }

    @Override
    public void onResume()
    {
        super.onResume();
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_7, this, mLoaderCallback);
    }

    public void onDestroy() {
        super.onDestroy();
        if (mOpenCvCameraView != null)
            mOpenCvCameraView.disableView();
    }

    public void onCameraViewStarted(int width, int height) {
    	
    	mWidth=width;
    	mHeight=height;
        mRgb = new Mat(height, width, CvType.CV_8UC3);
        mIntermediateMat = new Mat(height, width, CvType.CV_8UC3);
        mGrayMat = new Mat(height, width, CvType.CV_8UC1);
    }

    public void onCameraViewStopped() {
        mRgb.release();
        mGrayMat.release();
        mIntermediateMat.release();
    }

    public Mat onCameraFrame(Mat inputFrame) {
        final int viewMode = mViewMode;

        switch (viewMode) {
        case VIEW_MODE_GRAY:
            // input frame has gray scale format
        	selectfilter=1;
            inputFrame.copyTo(mRgb);
          //  Imgproc.cvtColor(mRgb, mRgb, Imgproc.COLOR_BGR2GRAY, 3);
            FindFeatures(mRgb.getNativeObjAddr(), mGrayMat.getNativeObjAddr(),  selectfilter, mWidth, mHeight);
          //  Imgproc.cvtColor(mGrayMat, mRgba, Imgproc.COLOR_GRAY2BGRA, 4);
            return mGrayMat;
         

            
        case VIEW_MODE_RGBA:
            // input frame has RBGA format
        	selectfilter=1;
            inputFrame.copyTo(mRgb);
           break;

            
        case VIEW_MODE_CANNY:
            // input frame has gray scale format
        	selectfilter=2;
            inputFrame.copyTo(mRgb);
          //  Imgproc.cvtColor(mRgb, mRgb, Imgproc.COLOR_BGR2GRAY, 3);
            FindFeatures(mRgb.getNativeObjAddr(), mGrayMat.getNativeObjAddr(),  selectfilter, mWidth, mHeight);
          //  Imgproc.cvtColor(mGrayMat, mRgba, Imgproc.COLOR_GRAY2BGRA, 4);
            return mGrayMat;
            
        case VIEW_MODE_FEATURES:
           //  input frame has RGBA format
        	selectfilter=0;
            inputFrame.copyTo(mRgb);
            FindFeatures(mRgb.getNativeObjAddr(), mRgb.getNativeObjAddr(), selectfilter,  mWidth, mHeight);
            break;
        }

        return mRgb;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        Log.i(TAG, "called onOptionsItemSelected; selected item: " + item);
        
       
        if (item == mItemPreviewRGBA) {
            mOpenCvCameraView.SetCaptureFormat(Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
            mViewMode = VIEW_MODE_RGBA;
        } else if (item == mItemPreviewGray) {
            mOpenCvCameraView.SetCaptureFormat(Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
            mViewMode = VIEW_MODE_GRAY;
        } else if (item == mItemPreviewCanny) {
            mOpenCvCameraView.SetCaptureFormat(Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
            mViewMode = VIEW_MODE_CANNY;
        } else if (item == mItemPreviewFeatures) {
            mOpenCvCameraView.SetCaptureFormat(Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);

            mViewMode = VIEW_MODE_FEATURES;
        }

        return true;
    }

    public native void FindFeatures(long matAddrRgba,long matAddrOutput, int select, int width, int height);
}
